<?php
require_once(ROOT_PATH.'service/WxpayAPI/lib/WxPay.Api.php');
require_once(ROOT_PATH.'service/WxpayAPI/example/WxPay.JsApiPay.php');


/**
* wxjsApi 微信jssdk 
* shouxue
* 2017/10/26 
*/
class orders extends Base
{
	public $uid;
	public $time;
	public $token;	
	public $order_id;
	public $stat = true;
	public $notify_url = '/php/v1/notify.php';

	//初始化
	function __construct($Ioc,$order_id)
	{
		$this->Ioc 	  = $Ioc;		

		$this->uid    	 = $_COOKIE['uid'];
		$this->time   	 = $_COOKIE['time'];
		$this->token  	 = $_COOKIE['token'];
		$this->order_id  = $order_id;

		//状态
		if(!$this->uid || !$this->time || !$this->token || !$this->order_id){
			$this->stat = false;
		}

	}


   	/**
	 * [main 主体]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	 public function main($mode,$stat = 'pay'){		
	
		if(!$this->stat || !$mode){
			$this->Ioc->Response->Json(211, '用户信息为空！','',$this ->out_mode);
		}

		//组合基本参数
		$url  =  HOST_URL.'/mobile/api/v1/wxPay.php';
		$data =  array();

		if($mode == 'order_info'){
			$data['mode']	  = $mode;
		}	
		if($mode == 'order_cancel'){
			$data['mode']	  = $mode;
		}	

		$is_wx = $this->is_wechat();
		$data['order_id'] = $this->order_id;
		$data['pay_attr'] = $is_wx; 
		$data['pay_mode'] = $stat; 			//pay:获取信息  on:支付中
		
		$result  = $this ->httpApi($url,$data);

		return $result;
	}


	/**
	 * [order_cancel 支付进行中]
	 * @Author   shouxue
	 * @DateTime 2017-11-20
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function order_list(){

		$is_wx = $this->is_wechat();
		$url   =  HOST_URL.'/mobile/api/v1/wxPay.php';
		$data  =  array();

		$data['mode']	  = 'order_list';
		$data['order_id'] = $this->order_id;
		$data['pay_attr'] = $is_wx; 

		$result  = $this ->httpApi($url,$data);
		return $result;
	}



	/**
	 * [order_cancel 支付宝跨平台支付码]
	 * @Author   shouxue
	 * @DateTime 2017-11-29
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function get_alipay_code(){

		$url   =  HOST_URL.'/mobile/api/v1/Public.php';
		$data  =  array();

		$data['mode']	  	= 'payCode';
		$data['order_id'] 	= $this->order_id;

		//效验参数
		$ip   =  getIp();
		$rel  =  strpos($ip,',');
		if($rel){
			$ipArr = explode(',',$ip);	
			$ip    = $ipArr[0];
		}

		$timestamp 			= time().mt_rand(1000,9999);
		$data['ip'] 	  	= $ip;
		$data['timestamp']	= $timestamp;
		$data['sign_code']	= serve_sign($timestamp);

		$result  = $this ->httpApi($url,$data);
		return $result;
	}




	/**
	 * [order_cancel 支付成功确认]
	 * @Author   shouxue
	 * @DateTime 2017-11-29
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function check_pay(){

		$url   =  HOST_URL.'/mobile/api/v1/wxPay.php';
		$data  =  array();

		$data['mode']	  = 'check_pay';
		$data['order_id'] = $this->order_id;

		$timestamp 			= time().mt_rand(1000,9999);
		$data['timestamp']	= $timestamp;
		$data['sign_code']	= serve_sign($timestamp);

		$result  = $this ->httpApi($url,$data);
		return $result;
	}

	/**
	 * [order_cancel 支付订单组装]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function order_data($data){


		$ip 		 = getIp();
		$time_start  = date("YmdHis");
		$time_expire = date("YmdHis",time() + 1200);	 //订单失效时间必须大于5分钟 此处默认20

		$title       = '小马生鲜';
		$detail      = '小马生鲜明细';

		//金额转换 元 ==》分
		$money =  $data['order_amount'] * 100;

		if(WxPay_Debug){
			$money   = 1;
			$title  .= '支付测试模式';  
		}

		$order['body']   		= $title;
		$order['detail'] 		= $detail;
		$order['attach'] 		= $ip;
		$order['out_trade_no'] 	= $data['order_sn'];
		$order['total_fee'] 	= $money; 
		$order['time_start'] 	= $time_start;
		$order['time_expire'] 	= $time_expire;
		$order['goods_tag']  	= '';
		$order['notify_url'] 	= BASE_URL.$this->notify_url;
		$order['ip']			= $ip;

		return $order;
	}



	/**
	 * [order_cancel 微信公众号订单生成]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function order_pay($data){

		//组装支付数据
        $order  = $this ->order_data($data);

		$tools  = new JsApiPay();
		$openId = $tools->GetOpenid();

		//缓存openId
		session_start();
		if($openId){
			$_SESSION['openId'] = $openId;
		}

		if(!$openId && isset($_SESSION['openId'])){
		 	$openId = $_SESSION['openId'];
		}

		//公众号支付
		$trade_type  = 'JSAPI';

		//统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody($order['body']);
		$input->SetAttach($order['attach']);
		$input->SetDetail($order['detail']);
		$input->SetOut_trade_no($order['out_trade_no']);
		$input->SetTotal_fee($order['total_fee']);
		$input->SetTime_start($order['time_start']);
		$input->SetTime_expire($order['time_expire']);
		$input->SetGoods_tag($order['goods_tag']);
		$input->SetNotify_url($order['notify_url']);
		$input->SetTrade_type($trade_type);
		$input->SetSpbill_create_ip($_SERVER['REMOTE_ADDR']);
		$input->SetOpenid($openId);

		logs('api---'.var_export($input,true));

		$orders = WxPayApi::unifiedOrder($input);
		if(!array_key_exists("prepay_id", $orders)){
			return $this->Ioc->Response->Min(510, $orders['err_code_des'], $orders); 
		}

		$jsApiParameters = $tools->GetJsApiParameters($orders);
		return $this->Ioc->Response->Min(200, '成功', $jsApiParameters); 
	}




	/**
	 * [order_cancel 微信订单生成]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function order_wap_pay($data){

		//组装支付数据
        $order  = $this ->order_data($data);

        //手机浏览器支付
		$trade_type  = 'MWEB';

		//统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody($order['body']);
		$input->SetAttach($order['attach']);
		$input->SetDetail($order['detail']);
		$input->SetOut_trade_no($order['out_trade_no']);
		$input->SetTotal_fee($order['total_fee']);
		$input->SetTime_start($order['time_start']);
		$input->SetTime_expire($order['time_expire']);
		$input->SetGoods_tag($order['goods_tag']);
		$input->SetNotify_url($order['notify_url']);
		$input->SetSpbill_create_ip($order['ip']);
		$input->SetTrade_type($trade_type);

		$orders = WxPayApi::unifiedOrder($input);
		if(!array_key_exists("prepay_id", $orders)){
			return $this->Ioc->Response->Min(510, $orders['err_code_des'], $orders); 
		}

		return $this->Ioc->Response->Min(200, '成功', $orders); 
	}


	/**
	 * [order_cancel 微信订单生成测试模型]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function order_pay2(){

		$tools  = new JsApiPay();
		$openId = $tools->GetOpenid();	

		//统一下单
		$input = new WxPayUnifiedOrder();
		$input->SetBody("test");
		$input->SetAttach("test");
		$input->SetDetail("mingxi");
		$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
		$input->SetTotal_fee("1");
		$input->SetTime_start(date("YmdHis"));
		$input->SetTime_expire(date("YmdHis", time() + 600));
		$input->SetGoods_tag("test");
		$input->SetNotify_url("http://paysdk.weixin.qq.com/example/notify.php");
		$input->SetTrade_type("JSAPI");
		$input->SetOpenid($openId);

		$order = WxPayApi::unifiedOrder($input);
		$jsApiParameters = $tools->GetJsApiParameters($order);

		return $this->Ioc->Response->Min(200, '成功2', $jsApiParameters); 
	}


	/**
	 * [httpApi ]
	 * @Author   shouxue
	 * @DateTime 2017-11-01
	 * @version  v1.0
	 * @return   bool|mixed
	 */
	public function httpApi($url,$data){
		
		//补充登录参数
		$data['uid'] 	  = $this->uid;
		$data['time'] 	  = $this->time;
		$data['token']    = $this->token;
	
		$result  = curlPost($url,$data); logs('api:'.$url.'----'.var_export($data,true).'----'.$result);
		if(!$result){
			return $this->Ioc->Response->Min(501, 'curl null', ''); 
		}

		if( preg_match('/^\xEF\xBB\xBF/', $result) ) {
	        $result = substr($result,3);
	    }

		$temp  = json_decode(trim($result),true);
		if(!$temp){
			return $this->Ioc->Response->Min(502, $result, ''); 
		}
		
		return $temp; 
	}	

}

